var mods = {
    internet:{},
    tool:{},
    intent:{},
    coor:{},
    view:{}
}

mods.view.waitViews = function (_views_content, _views_selector, _time, _frequency) {
    //等待一个控件数组找到，返回找到或者没找到
    //要找的控件数组
    _views_content = _views_content || []

    //要找的控件选择器
    _views_selector = _views_selector || ['text']

    //每次找控件的时间/毫秒
    _time = _time || 1000

    //找控件的次数/0此就是无限循环
    _frequency = _frequency || 1

    //自增1，用于记录do循环次数，如果等1就跳出do循环
    _frequency++

    do {

        maps = _views_content.map((_content, _index) => {

            switch (_views_selector[_index] || _views_selector[0]) {
                case 'text':
                    return text(_content).exists()
                case 'desc':
                    return desc(_content).exists()
                case 'id':
                    return id(_content).exists()
                default:
                    return text(_content).exists()
            }

        })

        if (maps.indexOf(false) == -1) {
            return true
        }

        if (_frequency != 0) {

            _frequency--

            if (_frequency == 1) {
                break
            }
        }
        sleep(_time)
    } while (_frequency >= 0)

    return false

}

mods.tool.getRandomNumOfSing = function (str_0, str_1) {
    //以str_1分割，取一个随机数
    switch (typeof(str_0)) {
        case 'number':
            //是数字
            return str_0
        case 'string':
            //是字符串
            if (str_0.indexOf(str_1) > -1) {
                num_min = str_0.split(str_1)[0]
                num_max = str_0.split(str_1)[1]
                return random(Number(num_min), Number(num_max))
            } else {
                return Number(str_0)
            }
        default:

            return 0;
    }
}

mods.coor.tap = function (x,y,z) {
    //兼容安卓版本同步点击
    if (z) {
        if (random(0,1) == 0) {
            x = x-z
        } else {
            x = x+z
        }

        if (random(0,1) == 0) {
            y = y-z
        } else {
            y = y+z
        }
    }
    if (device.sdkInt > 23) {
        click(x, y)
    } else {
        shell("input tap "+x+' '+y, true)
    }
}

mods.coor.swipe = function (x,y,x1, y1,delay, z) {
    //兼容安卓版本滑动
    if (z) {
        if (random(0,1) == 0) {
            x = x-z
        } else {
            x = x+z
        }

        if (random(0,1) == 0) {
            y = y-z
        } else {
            y = y+z
        }
    }
    if (device.sdkInt > 23) {
        swipe(x, y, x1, y1, delay)
    } else {
        shell("input swipe "+x+" "+y+" "+x1+" "+y1+" "+delay, true)
    }
}

mods.intent.openWeiBoUrl = function (url) {
    //打开微博浏览器
    app.startActivity({
        packageName: "com.sina.weibo",
        className: "com.sina.weibo.browser.WeiboBrowser",
        data: url,
        type: "Uri"
    });
}

mods.tool.md5 = function (string) {
    return java.math.BigInteger(1,java.security.MessageDigest.getInstance("MD5")
    .digest(java.lang.String(string).getBytes())).toString(16);
}

mods.tool.bjTime = function (_time) {
    //新建一个date对象
    date = new Date()
    _time = _time.split(' ')
    年 = _time[0].split('-')[0]
    月 = _time[0].split('-')[1]
    日 = _time[0].split('-')[2]
    时 = _time[1].split(':')[0]
    分 = _time[1].split(':')[1]
    秒 = _time[1].split(':')[2]
    if (date.getFullYear() >= Number(年)) {
        if (date.getMonth()+1 >= Number(月)) {
            if (date.getDate() >= Number(日)) {
                if (date.getHours() >= Number(时)) {
                    if (date.getMinutes() >= Number(分)) {
                        if (date.getSeconds() >= Number(秒)) {
                            return false
                        }
                    }
                }
            }
        }
    }
    return true
}

mods.internet.注册 = function (_user, _pass, _did) {
    _url = 'http://user.8-0000.com/api.php?act=reg'
    _data = {
        username : _user,
        password : _pass,
        did : _did
    }
    return waitHttpPost(_url, _data)
}

mods.internet.登录 = function (_user, _pass, _did) {
    _url = 'http://user.8-0000.com/api.php?act=login'
    _data = {
        username : _user,
        password : _pass,
        did : _did
    }
    return waitHttpPost(_url, _data)
}

mods.internet.充值 = function (_user, _pass, _did, _kami) {
    _url = 'http://user.8-0000.com/api.php?act=kami'
    _data = {
        username : _user,
        password : _pass,
        did : _did,
        kami : _kami
    }
    return waitHttpPost(_url, _data)
}

mods.internet.downloadCloudFiles = function (_path,_key,_local) {
    _url = 'http://dav.jianguoyun.com/dav/'
    _name = '17685034710@163.com'
    _code = base64(_name + ":" + _key)
    
    while (1) {
        try {
            _res = http.get(_url+_path ,{
                headers : {
                    "Authorization": "Basic " + _code,
                    "Content-Type": "text/plain;charset=UTF-8",
                    "Connection": "Keep-Alive",
                    "Accept-Encoding": "gzip",
                    "User-Agent": "okhttp/3.12.1"
                }
            })
        
            if (_res != null) {
                _res = _res.body.bytes()
                files.writeBytes(_local, _res)
                return true
            } else {
                log('返为空')
                return false
            }
            
        } catch (_err) {
            log('报错'+_err)
            return false
        }
    }
}

mods.internet.getCloudFiles = function (_path, _key) {
    _url = 'http://dav.jianguoyun.com/dav/'
    _name = '17685034710@163.com'
    _code = base64(_name + ":" + _key)
    
    try {
        _res = http.get(_url+_path ,{
            headers : {
                "Authorization": "Basic " + _code,
                "Content-Type": "text/plain;charset=UTF-8",
                "Connection": "Keep-Alive",
                "Accept-Encoding": "gzip",
                "User-Agent": "okhttp/3.12.1"
            }
        })
    
        if (_res != null) {
            _res = _res.body.string()
            return _res
        } else {
            return null
        }
        
    } catch (_err) {
        return null
    }
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>自定义函数>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

function waitHttpPost (_url, _data) {
    while (1) {
        try {
            _res = http.post(_url, _data)
            if (_res.statusCode == 200) {
                return _res.body.json()
            }
        } catch (_err) {
            log('waitHttpPost-->>  '+_err)
        }
    }
}

function base64(str) {
    return java.lang.String(android.util.Base64.encode(java.lang.String(str).getBytes(), 2));
}

module.exports = mods
